//
//  FMNetReachableTool.m
//  flutter_device_info_plugin
//
//  Created by 小琦 on 2025/2/24.
//

#import "FMNetReachableTool.h"
#import <SystemConfiguration/SCNetworkReachability.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

static NSString *notReachable = @"Other";

@implementation FMNetReachableTool

+ (nonnull NSString *)getNetReachableType {
    
    struct sockaddr_storage zeroAddress;
    
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.ss_len = sizeof(zeroAddress);
    zeroAddress.ss_family = AF_INET;
    
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags) {
        return notReachable;
    }
    
    BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
    BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
    if (isReachable && !needsConnection) { }else{
        return notReachable;
    }
    
    
    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired ) {
        
        return notReachable;
        
    } else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) {
        
        return [self getWWANType];
        
    } else {
        return @"WiFi";
    }
    
}

+ (NSString *)getWWANType {
    CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];
    
    NSString *currentTechnology;
    
    if (info && [info respondsToSelector:@selector(serviceCurrentRadioAccessTechnology)]) {
        if (@available(iOS 12.0, *)) {
            NSDictionary *radioDic = [info serviceCurrentRadioAccessTechnology];
            if (radioDic.allKeys.count) {
                currentTechnology = [radioDic objectForKey:radioDic.allKeys[0]];
            } else {
                return notReachable;
            }
        } else {
           
        }
    } else {
        return notReachable;
    }
    
    if (currentTechnology) {
        if (@available(iOS 14.1, *)) {
            if ([currentTechnology isEqualToString:CTRadioAccessTechnologyNRNSA] || [currentTechnology isEqualToString:CTRadioAccessTechnologyNR]) {
                return @"5G";
            }
        } else {
           
        }
        if ([currentTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
            return @"4G";
        } else if ([currentTechnology isEqualToString:CTRadioAccessTechnologyWCDMA] || [currentTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [currentTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] || [currentTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] || [currentTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] || [currentTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB] || [currentTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
            return @"3G";
        } else if ([currentTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentTechnology isEqualToString:CTRadioAccessTechnologyGPRS] || [currentTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
            return @"2G";
        } else {
            return notReachable;
        }
    } else {        
        return notReachable;
    }
}

@end
